<?php 
namespace Istat;


class Trasporto extends Importer 
{

	public $data_set = "DCCV_TRASPUB";
	protected $cities = array();
	public $type = 3;

	public function parsePlaceByYear($place)
	{
		$info = array();
		$info['tipologia'] = $this->type;

		$splitter = explode(" - ", $place->IDTIPO_DATO4->label);

		$info['citta'] = $splitter[1];
		$info['anno'] = $splitter[3];
	
		$info['geo'] = $this->geocode($info['citta']);
	
		$info['domanda_trasporto_procapite'] = (float) isset($place->IDTIPO_DATO4->value[0])?$place->IDTIPO_DATO4->value[0]:0;
		return $info;
	}


	public function parsePlace($place)
	{

		$info = new Trasporto();
		$splitter = explode(" - ", $place->IDTIPO_DATO4->label);

                $info->citta = $splitter[1];
                $info->anno = $splitter[3];
                $info->geo = $this->geocode($info->citta);

                $info->value['trasporto_procapite'] = (float) isset($place->IDTIPO_DATO4->value[0])?$place->IDTIPO_DATO4->value[0]:0;
		return $info;
	}

    public function generateUrls($geo = false)
    {
        $response = $this->browser->get($this->service_url.$this->data_set);
        $services = json_decode($response->getContent(), true);

        $this->services = $this->sanitize($services);

        $parsed = array();
        foreach ($this->services['Territorio'] as $place)
        {
            if ($geo) $place['geo'] = $this->geocode($place['Desc']);

		    foreach($this->services['Misura'] as $monitoraggio)
		    {
                        	foreach($this->services['Anno'] as $anno)
                        	{

                                  $parsed[] = 'http://apistat.istat.it/?q=getdatajson&dataset='.$this->data_set.'&dim='.$place['Cod'].',0,'.$monitoraggio['Cod'].','.$anno['Cod'];

                        	}
            }
	    }

            return $parsed;
    }


}
